package net.llz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import net.llz.model.VideoDO;
import net.llz.mapper.VideoMapper;
import net.llz.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 二当家小D
 * @since 2022-09-16
 */
@Service
@Slf4j
public class VideoServiceImpl  implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    /**
     *分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Map<String, Object> findAll(int page, int size) {

        Page<VideoDO>pageInfo = new Page<>(page,size);
        IPage<VideoDO> videoDOIPage =videoMapper.selectPage(pageInfo,new QueryWrapper<VideoDO>());

        Map<String,Object> pageMap=new HashMap<>(6);
        pageMap.put("video_total",videoDOIPage.getTotal());
        pageMap.put("video_page",videoDOIPage.getPages());
        pageMap.put("video_data",videoDOIPage.getRecords());
        pageMap.put("video_current",videoDOIPage.getCurrent());
        pageMap.put("video_total",videoDOIPage.getTotal());
        pageMap.put("video_size",videoDOIPage.getSize());

        return pageMap;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<VideoDO> selectAll() {

        return videoMapper.selectList(new QueryWrapper<VideoDO>());
    }

    /**
     * 根据id查询所有接口
     * @param id
     * @return
     */
    @Override
    public VideoDO findByid(int id) {

        return videoMapper.selectById(id);
    }


    /**
     * 根据id修改
     * @param videoDO
     * @return
     */
    @Override
    public int update(VideoDO videoDO) {

     int rows=  videoMapper.update(videoDO,new QueryWrapper<VideoDO>().eq("id",videoDO.getId()));
      //  videoMapper.updateById(videoDO);

        return rows;
    }

    /**
     * 保存接口
     * @param id
     * @return
     */
    @Override
    public int delete(int id) {

        return videoMapper.deleteById(id);
    }

    @Override
    public int saveVideo(VideoDO videoDO) {
        int rows =videoMapper.insert(videoDO);
        System.out.println("row_id:"+videoDO.getId());
        return rows;
    }

    @Override
    public int update_by_id(VideoDO videoDO) {
        return videoMapper.updateById(videoDO);
    }


}
